package br.com.amadeus.android.adapters;

import java.util.ArrayList;
import java.util.Collections;

import br.com.amadeus.android.activities.home.R;
import br.com.amadeus.android.entities.Message;
import br.com.amadeus.android.util.ConstantsAmadeusDroid;
import br.com.amadeus.android.util.Util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ForumMessageListAdapter extends BaseAdapter {
	
	protected static final String CATEGORIA = "livro";
	private Context context;
	private ArrayList<Message> messageList;
	
	public ForumMessageListAdapter(Context context,
			ArrayList<Message> messageList) {
		super();
		this.context = context;
		Collections.reverse(messageList);
		this.messageList = messageList;
	}

	public int getCount() {
		return this.messageList.size();
	}

	public Object getItem(int position) {
		Message message = this.messageList.get(position);
		return message;
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int positon, View view, ViewGroup viewGroup) {
		
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View v = inflater.inflate(R.layout.forum_messages_list, null);
		
		Message message = this.messageList.get(positon);
		TextView tvBody = (TextView) v.findViewById(R.id.tvBodyMessage);
		TextView tvAuthor = (TextView) v.findViewById(R.id.tvAuthor);
		TextView tvDate = (TextView) v.findViewById(R.id.tvDateMessage);
		
		tvBody.setText(message.getBody());
		tvAuthor.setText(message.getPersonName() + ",");
		tvDate.setText(Util.getDataHoraFormatoCompleto(message.getDate()));
		
		ImageView ivAuthor = (ImageView) v.findViewById(R.id.ivAuthorMessageForum);
		
		try {
			final Bitmap imagem = BitmapFactory.decodeFile("/sdcard" + ConstantsAmadeusDroid.ROOT_ARCHIVES + "/" + 
					message.getPersonLogin() + ".png");
			if (imagem != null) {
				ivAuthor.setImageBitmap(imagem);
			}
		} catch (Exception e) {
			Log.e(CATEGORIA, e.getMessage(), e);
		}
		
		return v;
	}

}
